package com.macrounion.atv.service.service.impl;

import com.google.common.collect.Lists;
import com.macrounion.atv.service.dto.resp.SpotListRespDto;
import com.macrounion.atv.service.dto.resp.SpotRespDto;
import com.macrounion.atv.service.entity.Device;
import com.macrounion.atv.service.entity.Spot;
import com.macrounion.atv.service.entity.ext.SpotExt;
import com.macrounion.atv.service.mapper.DeviceMapperExt;
import com.macrounion.atv.service.mapper.SpotMapper;
import com.macrounion.atv.service.mapper.SpotMapperExt;
import com.macrounion.atv.service.service.SpotService;
import com.macrounion.base.service.service.impl.BaseServiceImpl;
import org.springframework.beans.BeanUtils;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;
import org.springframework.util.CollectionUtils;
import org.springframework.util.StringUtils;

import javax.swing.text.StyledEditorKit;
import java.awt.*;
import java.util.ArrayList;
import java.util.List;
import java.util.stream.Collectors;

@Service
public class SpotServiceImpl extends BaseServiceImpl<SpotMapper, Spot> implements SpotService {

    @Autowired
    private SpotMapperExt spotMapperExt;
    @Autowired
    private DeviceMapperExt deviceMapperExt;

    @Override
    public List<Spot> getSpotByParentId(String parentId) {
        return spotMapperExt.findSpotByparentId(parentId);
    }

    @Override
    public List<SpotRespDto> getTreeSpot(String parentSpotId, Integer isLoadDevice,Integer isLoadPing) {
        List<SpotRespDto> resp=Lists.newArrayList();
        List<Spot> spotList=spotMapperExt.findSpotByparentId(parentSpotId);

        List<Device> deviceList= Lists.newArrayList();
        if(isLoadDevice==1){
            deviceList=deviceMapperExt.getDeviceByParentSpotId(parentSpotId);
        }


        if(!CollectionUtils.isEmpty(spotList)){
            for(Spot p : spotList){
                SpotRespDto spotDto=new SpotRespDto();
                BeanUtils.copyProperties(p, spotDto);
                spotDto.setIsDevice(false);
                resp.add(spotDto);
            }
        }

        if(isLoadDevice==1 && !CollectionUtils.isEmpty(deviceList)){
            List<String> ids=deviceList.stream().map(Device::getId).collect(Collectors.toList());
            List<Device> rePingDeviceList=Lists.newArrayList();
            rePingDeviceList=deviceMapperExt.getRePingByIds(ids);

            for(Device d : deviceList){
                if(!CollectionUtils.isEmpty(rePingDeviceList) && isLoadPing==0 && rePingDeviceList.stream().filter(p->p.getId().equals(d.getId())).count()>0){
                    continue;
                }
                SpotRespDto spotDto=new SpotRespDto();
                BeanUtils.copyProperties(d, spotDto);
                spotDto.setIsDevice(true);
                spotDto.setCode(d.getDeviceCode());
                resp.add(spotDto);
            }
        }

        return resp;
    }

    @Override
    public List<SpotListRespDto> getSpotListBySpot(SpotExt spotext) {
        return spotMapperExt.getSpotListBySpot(spotext);
    }

    @Override
    public List<Spot> getAllSpot() {
        return spotMapperExt.getAllSpot();
    }

    @Override
    public int findCount(Spot spot) {
        return spotMapperExt.findCount(spot);
    }

    @Override
    public List<Spot> getAllParentSpotById(String spotId) {
        return spotMapperExt.getAllParentSpotById(spotId);
    }

    @Override
    public List<Spot> getChildrenSpotById(String spotId) {
        List<Spot> children=Lists.newArrayList();
        List<Spot> list=spotMapperExt.findSpotByparentId(spotId);

        if(CollectionUtils.isEmpty(list))
            return children;
        list.forEach(p->{
            children.addAll(getChildrenSpotById(p.getId()));
            children.add(p);
        });

        return children;


    }
}